package com.sws.worktive.dialogfragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseDialogFragment extends DialogFragment {

	private OnDismissListener mOnDismissListener;
	
	private final String KEY_CANCEL_TOUCH_OUTSIDE = "key_cancel_touch_outside";
	
//================================API================================
    public void unRegisterOndismissListener(){
		setOnDismissListener(null); 
	}
	
    public void setOnDismissListener(OnDismissListener onDismissListener){
    	mOnDismissListener = onDismissListener;
    }
    
    public void setCancelTouchOutside(boolean cancelTouchOutside){
    	Bundle bundle = new Bundle();
    	bundle.putBoolean(KEY_CANCEL_TOUCH_OUTSIDE, cancelTouchOutside);
    	setArguments(bundle);
    }

//================================Life Cycle================================
	 
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, android.R.style.Theme_Dialog);
    }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		getDialog().setCanceledOnTouchOutside(getArguments().getBoolean(KEY_CANCEL_TOUCH_OUTSIDE));
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
		if (mOnDismissListener != null) mOnDismissListener.onDismiss(dialog);
	}

}
